//
//  GridPersonnelAddViewController.swift
//  grid-ios-app
//
//  Created by yanlu on 2024/4/3.
//

import UIKit
import SnapKit
import Toast_Swift

class GridPersonnelAddViewController: GridBaseViewController {

    let persionModel: GridPersonnelCellModel?
    
    private let addModel: GridPersonnelAddViewwModel = GridPersonnelAddViewwModel()
    
    init(persionModel: GridPersonnelCellModel?) {
        self.persionModel = persionModel
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var addView: GridPersonnelAddView = {
        let topV = GridPersonnelAddView(frame: .zero)
        topV.delegate = self
        view.addSubview(topV)
        return topV
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        navigationItem.title = "添加人员信息"
        addView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
        addView.persionModel = self.persionModel
        if persionModel != nil {
            addModel.gridModel = persionModel?.gridModel
            addModel.name = persionModel?.name ?? ""
            addModel.phoneNum = persionModel?.phoneNum ?? ""
            if let ge: String = persionModel?.gender {
                if ge == "男" {
                    addModel.gender = 0

                } else {
                    addModel.gender = 1
                }
            }
            addModel.management = persionModel?.management ?? ""
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension GridPersonnelAddViewController: GridPersonnelAddViewDelegate {
    func gridPersonnelAddViewClickGender(tag: Int) {
        addModel.gender = tag
    }
    
    func gridPersonnelAddViewTextFieldChange(text: String?, type: Int) {
        if type == 1 {
            addModel.name = text ?? ""
        }else {
            addModel.phoneNum = text ?? ""
        }
    }
    
    func gridPersonnelAddViewSelectedClick(type: Int) {
        if type == 1 {
            let vc: GridManagementListViewController = GridManagementListViewController()
            weak var weakSelf = self
            vc.backAction = {  (model: GridModel) -> Void in
                weakSelf?.addModel.gridModel = model
                weakSelf?.addView.gridModel = model
            }
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    func gridPersonnelAddViewSaveClick() {
        if let message = self.addModel.jundgeIsSave() {
            self.view.makeToast(message, duration: 3.0, position: .center)
        } else {
            // 调用接口逻辑处理
            print("调用接口逻辑处理")
        }
    }
    
    
}
